package managedbean.CRM.Discount;

import entity.CRM.Discount;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.DiscountBeanLocal;

@ManagedBean
@ViewScoped
public class EditDiscountManagerBean implements Serializable
{
    @EJB
    private DiscountBeanLocal discountBeanLocal;
    private Discount discountToEdit;
    private String statusMessage;

    public Discount getDiscountToEdit() {
        return discountToEdit;
    }
    public void setDiscountToEdit(Discount discountToEdit) {
        this.discountToEdit = discountToEdit;
    }

    public String getStatusMessage() {
        return statusMessage;
    }
    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public EditDiscountManagerBean() {}
    
    public void initView(PhaseEvent event){
        if(discountToEdit == null)
        {            
            discountToEdit = (Discount)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedDiscount");            
            System.out.println("Product Type "+ discountToEdit.getProductType());
        }
    }
    
    public void updateDiscount(ActionEvent event){
        discountBeanLocal.modifyDiscount(discountToEdit.getDiscountId(), discountToEdit.getLowerLimit(), discountToEdit.getHigherLimit(), discountToEdit.getDiscountRate());
        System.out.println("ProductType after edit:"+discountToEdit.getProductType());
        statusMessage = "Discount updated Successfully"; 
 
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Update Discount Result: " +statusMessage+"", "")); 
    }    
}
